package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.domain.basic.TerminalUpgradePack;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.common.utils.HttpClientUtils;
import cc.yugu.yhdw.common.utils.YhdwUtils;
import cc.yugu.yhdw.webserver.config.AppConfig;
import cc.yugu.yhdw.webserver.persistence.basic.TerminalUpgradePackMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
 * 终端升级业务
 * 
 * @Date:Created in 2018/7/5 15:14
 * @Author:
 * @Modified By: zjn 2018/7/5 15:14
 */ 
@Service
public class TerminalUpgradePackService {
    @Autowired
    TerminalUpgradePackMapper terminalUpgradePackMapper;

    @Autowired
    AppConfig config;
    /**
    * 根据主键查询表 bas_terminal_upgrade_pack
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.basic.TerminalUpgradePack
    * @Author By: zjn 2018/7/5  15:14
    */
    public TerminalUpgradePack find(int id) {
        return terminalUpgradePackMapper.find(id);
    }
    /**
    * 分页查询
    *
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/5  15:14
    */
    public Page findPage(TerminalUpgradePack search) {
        Page page = search.buildPage();
        page.setTotalItems(terminalUpgradePackMapper.findPageCount(search));
        search.setBeginIndex(page.getOffset());
        page.setResult(terminalUpgradePackMapper.findPageResult(search));
        return page;
    }
    /**
    * 插入(新增？)
    *
    * @Param: [entity, fileMap]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/5  15:14
    */
    public ExtResult create(TerminalUpgradePack entity, Map<String, File> fileMap) {
        if (!fileMap.isEmpty()) {
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("AGENT_ID", String.format("%d", entity.getAgentId()));
            // 请求
            HttpClientUtils.HttpResp httpResp = HttpClientUtils.uploadFile(config.staticUrl + "/security/basic_upload/terminal_upgrade.htm", fileMap, paramMap, YhdwUtils.buildHttpHeader(System.currentTimeMillis(), config.uploadSalt));
            Map map = null;
            try {
                map = (Map) YhdwUtils.decodeJson(httpResp.content, Map.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Map data = (Map) map.get("data");
            // 获取路径
            String filePath = (String) data.get("filePath");
            if (StringUtils.isNotEmpty(filePath)) {
                entity.setFilePath(filePath);
            }
        }
        terminalUpgradePackMapper.insert(entity);
        return ExtResult.successResult();
    }
    /**
    * 更新终端升级信息
    *
    * @Param: [entity, fileMap]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/5  15:25
    */
    public ExtResult update(TerminalUpgradePack entity, Map<String, File> fileMap) throws IOException {
        // 根据主键查询表 bas_terminal_upgrade_pack
        TerminalUpgradePack upgradePack = terminalUpgradePackMapper.find(entity.getId());
        if(upgradePack == null) {
            return ExtResult.failResult("记录不存在");
        }
        entity.setUpdateTime(new Date());

        if (!fileMap.isEmpty()) {
            Map<String, String> paramMap = new HashMap<String, String>();
            paramMap.put("AGENT_ID", String.format("%d", entity.getAgentId()));

            HttpClientUtils.HttpResp httpResp = HttpClientUtils.uploadFile(config.staticUrl + "/security/basic_upload/terminal_upgrade.htm", fileMap, paramMap, YhdwUtils.buildHttpHeader(System.currentTimeMillis(), config.uploadSalt));
            Map map = (Map) YhdwUtils.decodeJson(httpResp.content, Map.class);
            Map data = (Map) map.get("data");

            String filePath = (String) data.get("filePath");
            if (StringUtils.isNotEmpty(filePath)) {
                entity.setFilePath(filePath);
            }
        }
        terminalUpgradePackMapper.update(entity);


        return ExtResult.successResult();
    }
    /**
    * 根据主键删除
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/5  15:27
    */
    public ExtResult delete(int id) {
        terminalUpgradePackMapper.delete(id);
        return ExtResult.successResult();
    }
}
